package com.zxp.quartz.job;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author: qingshan
 * @Date: 2018/10/15 19:40
 * @Description: 咕泡学院，只为更好的你
 * @PersistJobDataAfterExecution 任务执行完后将dataMap持久化，但对trigger里面的dataMap不起作用
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class MyJob2 implements Job {
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JobDataMap triggerJobDataMap = jobExecutionContext.getTrigger().getJobDataMap();
        JobDataMap jobDetailDataMap = jobExecutionContext.getJobDetail().getJobDataMap();

        jobDetailDataMap.put("count1",jobDetailDataMap.getInt("count1")+1);
        triggerJobDataMap.put("count",triggerJobDataMap.getInt("count")+1);

        System.out.println("jobDetailDataMap count1:"+jobDetailDataMap.getInt("count1"));
        System.out.println("trigger dataMap count:"+triggerJobDataMap.getInt("count"));

        try {
            System.out.println(" " + sf.format(date) + "   》》》》任务2开始执行了，需要5秒");
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
